class Solution:
    def numIslands(self, grid: list) -> int:
        if grid == []:
            return 0
        res, self.grid = 0, grid
        self.height, self.length = len(grid), len(grid[0])
        for i in range(self.height):
            for j in range(self.length):
                if self.grid[i][j] == "1":
                    res += 1
                    self.search_deep(i, j)
        return res

    def search_deep(self, i, j):
        self.grid[i][j] = "0"
        if i > 0 and self.grid[i-1][j] == "1":
            self.search_deep(i-1, j)
        if i < self.height - 1 and self.grid[i+1][j] == "1":
            self.search_deep(i+1, j)
        if j > 0 and self.grid[i][j-1] == "1":
            self.search_deep(i, j-1)
        if j < self.length - 1 and self.grid[i][j+1] == "1":
            self.search_deep(i, j+1)


a = Solution()
print(a.numIslands([["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]]))
